package cn.fintecher.pangolin.service.manage.request;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 查询系统日志请求参数
 * @Date: 10:36 2019/9/25
 */

@Data
public class FindSystemLogRequest {
    @ApiModelProperty(value = "操作人")
    private String operator;

    @ApiModelProperty(value = "描述")
    private String remark;

    @ApiModelProperty(value = "最小操作时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minOperateTime;

    @ApiModelProperty(value = "最大操作时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxOperateTime;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        if (StringUtils.isNotBlank(this.operator)) {
            builder.must(QueryBuilders.matchPhraseQuery("operatorName.keyword", StringUtils.trim(this.operator)));
        }
        if (StringUtils.isNotBlank(this.remark)) {
            builder.must(QueryBuilders.matchQuery("remark.keyword", this.remark));
        }
        if (Objects.nonNull(this.minOperateTime)) {
            builder.must(QueryBuilders.rangeQuery("operateTime").gte(this.minOperateTime.getTime()));
        }
        if (Objects.nonNull(this.maxOperateTime)) {
            builder.must(QueryBuilders.rangeQuery("operateTime").lte(this.maxOperateTime.getTime() + 86400000));
        }
        return builder;
    }
}
